package cn.com.hhrcw.web;

import cn.com.hhrcw.entity.OpShielding;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.mapstruct.OpShieldingConvert;
import cn.com.hhrcw.params.OpShieldingParams;
import cn.com.hhrcw.service.IOpShieldingService;
import cn.com.hhrcw.utils.SubjectUtils;
import cn.com.hhrcw.vo.OpShieldingVO;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.base.entity.AttributesValueParams;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.function.Function;

/**
 * 用户屏蔽公司表
 *
 * @author jiangdong
 * @since 2020-12-28
 */
@RestController
@Slf4j
@RequestMapping("/personal/v1/opShielding")
@Api(tags = "用户屏蔽公司表")
@Validated
public class OpShieldingController extends JeecgController<OpShielding, IOpShieldingService> {

    @Autowired
    private IOpShieldingService opShieldingService;

    @Autowired
    private OpShieldingConvert opShieldingConvert;

    /**
     * 分页列表查询
     *
     * @param params 请求参数
     * @return
     */
    @GetMapping(value = "/list")
    @AutoLog("用户屏蔽公司表-分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PermissionData
    public Result<Page<OpShieldingVO>> list(OpShieldingParams params, HttpServletRequest request) {
        QueryWrapper<OpShielding> queryWrapper = QueryGenerator.initQueryWrapper(opShieldingConvert.toEntity(params), request.getParameterMap());
        queryWrapper.eq("del_flag", false);
        if (StringUtils.isBlank(params.getField())) {
            queryWrapper.orderByDesc("create_time");
        } else {
            queryWrapper.orderBy(true, params.getSort(), toUnderlineName(params.getField()));
        }
        IPage<OpShielding> page = opShieldingService.page(params.page(), queryWrapper);
        //分页结果转换器
        Function<OpShielding, OpShieldingVO> fun = opShielding -> opShieldingConvert.toVO(opShielding);

        return Result.ok((Page) page.convert(fun));
    }

    /**
     * 根据id查询
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/queryById")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    @PermissionData
    public Result<OpShieldingVO> queryById(String id) {
        OpShielding opShielding = opShieldingService
            .lambdaQuery()
            .eq(OpShielding::getId, id)
            .eq(OpShielding::getDelFlag, false)
            .one();
        if (opShielding == null) {
            throw new JeecgBootException("数据不存在");
        }
        return Result.ok(opShieldingConvert.toVO(opShielding));
    }

    /**
     * 取消屏蔽
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/unAdd")
    @AutoLog("用户屏蔽公司表-取消屏蔽")
    @ApiOperation(value = "取消屏蔽", notes = "取消屏蔽")
    public Result<?> unAdd(@RequestBody @Valid OpShieldingParams params) {

        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        boolean saved = service.lambdaUpdate().set(OpShielding::getDelFlag, true).eq(OpShielding::getComId, params.getComId())
            .eq(OpShielding::getUserId, sysUser.getId()).update();
        return saved ? Result.ok() : Result.error("添加失败");
    }

    /**
     * 添加
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/add")
    @AutoLog("用户屏蔽公司表-添加")
    @ApiOperation(value = "添加", notes = "添加")
    public Result<?> add(@RequestBody @Valid OpShieldingParams params) {

        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        OpShielding opShielding = opShieldingConvert.toEntity(params);
        opShielding.setCreateBy(sysUser.getUsername());
        opShielding.setCreateTime(DateUtil.date());
        opShielding.setDelFlag(false);
        opShielding.setUserId(sysUser.getId());
        //opShielding.setNo(super.sort("op_shielding", Constant.NO_SEED));//FIXME: 编号,可根据情况隐藏
        boolean saved = opShieldingService.save(opShielding);
        return saved ? Result.ok() : Result.error("添加失败");
    }

    /**
     * 编辑
     *
     * @param params 请求参数
     * @return
     */
    @PostMapping(value = "/edit")
    @AutoLog("用户屏蔽公司表-编辑")
    @ApiOperation(value = "编辑", notes = "编辑")
    @RequiresPermissions("opShielding:edit")
    public Result<?> edit(@RequestBody @Valid OpShieldingParams params) {
        SysUser sysUser = (SysUser) SubjectUtils.getSubject();

        OpShielding opShielding = opShieldingConvert.toEntity(params);
        opShielding.setUpdateBy(sysUser.getUsername());
        opShielding.setUpdateTime(DateUtil.date());

        boolean updated = opShieldingService.updateById(opShielding);
        return updated ? Result.ok() : Result.error("编辑失败");
    }

    /**
     * 批量删除
     *
     * @param id 数据id
     * @return
     */
    @GetMapping(value = "/delete")
    @AutoLog("用户屏蔽公司表-批量删除")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @RequiresPermissions("opShielding:delete")
    public Result delete(String[] id) {
        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        LambdaUpdateWrapper<OpShielding> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.in(OpShielding::getId, id)
            .set(OpShielding::getDelFlag, true)
            .set(OpShielding::getUpdateBy, sysUser.getUsername())
            .set(OpShielding::getUpdateTime, DateUtil.date());

        boolean deleted = opShieldingService.update(updateWrapper);
        return deleted ? Result.ok() : Result.error("删除失败");
    }

    /**
     * 属性值是否重复
     *
     * @return
     */
    @Override
    @GetMapping("/propertyRepeat")
    @ApiOperation(value = "属性值是否重复", notes = "属性值是否重复")
    public Result propertyIsRepeat(@Valid AttributesValueParams params) {
        return super.propertyIsRepeat(params);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param params
     */
    @SneakyThrows
    @GetMapping(value = "/exportXls")
    @ApiOperation(value = "导出excel", notes = "导出excel")
    public Result exportXls(HttpServletRequest request, OpShieldingParams params) {
        OpShielding opShielding = opShieldingConvert.toEntity(params);
        opShielding.setDelFlag(false);
        QueryWrapper<OpShielding> queryWrapper = QueryGenerator.initQueryWrapper(opShielding, request.getParameterMap());
        List<OpShielding> data = opShieldingService.list(queryWrapper);
        ExportParams exportParams = new ExportParams("用户屏蔽公司表", "用户屏蔽公司表");
        String url = super.exportXls(exportParams, OpShieldingVO.class, opShieldingConvert.toVO(data));
        return Result.ok((Object) url);
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/importExcel")
    @ApiOperation(value = "通过excel导入数据", notes = "通过excel导入数据")
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, OpShielding.class);
    }

}
